package com.cote.douhahou.pretloan.msg.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.cote.douhahou.pretloan.beans.AppAkomapalendConfigCustomize;
import com.cote.douhahou.pretloan.beans.BaAkomapalendnkAccount;
import com.cote.douhahou.pretloan.beans.OtheAkomapalendrsInfo;
import com.cote.douhahou.pretloan.beans.ProcesAkomapalendsInfoData;
import com.cote.douhahou.pretloan.msg.AccAkomapalendountList;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.R;
import com.cote.douhahou.pretloan.msg.BinAkomapalenddAccoChannelDialog;
import com.cote.douhahou.pretloan.msg.BindAccountAkomapalendDialog;
import com.cote.douhahou.pretloan.msg.DeleteAccountDiAkomapalendalog;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAccoAkomapalenduntViewManager extends BaseProcAkomapalendessWidgetManager {

    private final FragmentActivity activ_akomapalendity;

    private LinearLayout accoun_akomapalendtGroup_akomapalendLayout;

    private ArrayList<BaAkomapalendnkAccount> accoun_akomapalendts = new ArrayList<>();

    private final View.OnClickListener addAcco_akomapalenduntCl;

    private String bindChann_akomapalendelMobile = "";

    public BindAccoAkomapalenduntViewManager(View view, ProcesAkomapalendsInfoData data, String pageTag){
        super(view,data,pageTag);
        activ_akomapalendity = (FragmentActivity) view.getContext();
        addAcco_akomapalenduntCl = v -> {
            HttpHelper.inst().submitEvent(pa_akomapalendge,"ADD_CLICK","");
            new BindAccountAkomapalendDialog(activ_akomapalendity, pa_akomapalendge,
                    mobile -> {
                        bindChann_akomapalendelMobile = mobile;
                        HttpHelper.inst().fAccountList(activ_akomapalendity,this::refreshAcc_akomapalendountList);
                    }
            ).show();
        };
     }

    @Override
    protected void in_akomapalendit(){
        accoun_akomapalendtGroup_akomapalendLayout = mVi_akomapalendew.findViewById(R.id.ipbfa_layout_account_group);
        TextView tipsTitleTv_akomapalend = mVi_akomapalendew.findViewById(R.id.ipbfa_tv_tips_title);
        TextView tipsDescTv_akomapalend = mVi_akomapalendew.findViewById(R.id.ipbfa_tv_tips_desc);

        mVi_akomapalendew.findViewById(R.id.ipbfa_btn_add_account).setOnClickListener(v -> {
            addAcco_akomapalenduntCl.onClick(v);
        });

        String tipsTitle = AppAkomapalendConfigCustomize.instance().getBindAccountTipsTitle_akomapalend();
        String tipsDesc = AppAkomapalendConfigCustomize.instance().getBindAccountTipsDesc_akomapalend();

        if (!TextUtils.isEmpty(tipsTitle) && !TextUtils.isEmpty(tipsDesc)){
            tipsTitleTv_akomapalend.setText(tipsTitle);
            tipsDescTv_akomapalend.setText(tipsDesc);
        }

    }

    @Override
    public boolean checkI_akomapalendnput() {
        return !accoun_akomapalendts.isEmpty();
    }

    @Override
    public String getInp_akomapalendut() {
        return String.valueOf(accoun_akomapalendts.size());
    }

    //更新列表
    public void refreshAcc_akomapalendountList(){
        accoun_akomapalendtGroup_akomapalendLayout.removeAllViews();

        accoun_akomapalendts = AccAkomapalendountList.instance().getBankAccountList_akomapalend();

        int num = 1;
        int i= accoun_akomapalendts.size()-1;
        while (i> accoun_akomapalendts.size()-3 || i>=0){

            if (i>=0) addAcc_akomapalendount(accoun_akomapalendts.get(i),num++);
            else {
                View view = View.inflate(mVi_akomapalendew.getContext(), R.layout.item_edit_account, null);
                TextView titleView = view.findViewById(R.id.iaafa_tv_title);
                titleView.append(" "+(num++));
                view.findViewById(R.id.iaafa_view_click).setOnClickListener(addAcco_akomapalenduntCl);
                accoun_akomapalendtGroup_akomapalendLayout.addView(view);
            }

            i--;
        }

        //更新列表后，判断是否需要绑定运营商
        if (!TextUtils.isEmpty(bindChann_akomapalendelMobile) ){
            if (OtheAkomapalendrsInfo.instance().getAddBindFlag_akomapalend()){
                new BinAkomapalenddAccoChannelDialog(activ_akomapalendity, bindChann_akomapalendelMobile,
                        null,
                        () -> HttpHelper.inst().newAccount(
                                activ_akomapalendity,
                                bindChann_akomapalendelMobile,
                                bindChann_akomapalendelMobile,
                                OtheAkomapalendrsInfo.instance().getChannel_akomapalend(),
                                () -> HttpHelper.inst().fAccountList(activ_akomapalendity,this::refreshAcc_akomapalendountList),
                                false
                        )

                ).show();
            }
        }

    }

    //添加卡
    private void addAcc_akomapalendount(BaAkomapalendnkAccount account, int num){

        if (account == null) return;

        String desc = account.getWalletDesc_akomapalend();

        View accountView = View.inflate(mVi_akomapalendew.getContext(), R.layout.item_account, null);
        TextView titleTv = accountView.findViewById(R.id.iafa_tv_title);
        ImageView logoIv = accountView.findViewById(R.id.iafa_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iafa_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iafa_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iafa_ib_delete_account);

        titleTv.append(" "+num);
        mobileTv.setText(account.getWalletMobile_akomapalend());
        channelTv.setText(account.getWalletDesc_akomapalend());

        //删除卡
        deleteIb.setOnClickListener(v -> {
            HttpHelper.inst().submitEvent(pa_akomapalendge,"DELETE_CLICK",account.getWalletMobile_akomapalend());
            new DeleteAccountDiAkomapalendalog(activ_akomapalendity,account, pa_akomapalendge,
                    () -> HttpHelper.inst().fAccountList(activ_akomapalendity,this::refreshAcc_akomapalendountList)).show();
        });

        accoun_akomapalendtGroup_akomapalendLayout.addView(accountView);

        
        if (AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_akomapalend())) return;
                    if (!AppAkomapalendConfigCustomize.instance().hasAccountChannel_akomapalend()) return;
                    URL url = new URL(account.getWalletLogo_akomapalend());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppAkomapalendConfigCustomize.instance().setChannelLogoByDesc_akomapalend(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppAkomapalendConfigCustomize.instance().getChannelLogoByDesc_akomapalend(desc));

    }

}
